package edu.nazarevych.portal.dao;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;

import org.hibernate.classic.Session;
import org.hibernate.transform.Transformers;

import edu.nazarevych.portal.entities.Stations;
import edu.nazarevych.portal.entities.User;

public class StationsDao extends BaseDao<Stations> {

	public StationsDao() {
		super(Stations.class);
		session = HibernateSessionFactory.sessionFactory.openSession();
	}

	@SuppressWarnings("unchecked")
	public List<Stations> findAll() {
		openSession();
		List<Stations> list = (List<Stations>) session
				.createSQLQuery("SELECT  * FROM  diploma.tbl_station")
				.setResultTransformer(Transformers.aliasToBean(Stations.class))
				.list();
		closeSession();
		return list;
	}

	public Stations getStationByName(String name) {
		openSession();
		final String query = "SELECT  * FROM  diploma.tbl_station WHERE name LIKE '[name]'";
		Stations stations = (Stations) session.createSQLQuery(query.replace("[name]", name))
				.setResultTransformer(Transformers.aliasToBean(Stations.class))
				.uniqueResult();
		closeSession();
		return stations;
	}

	public List<Stations> getUserStation(User users) {
		openSession();
		List<Stations> list = (List<Stations>) session
				.createSQLQuery(
						"SELECT s.id, `Name`, `Longitude`, `Latitude`, `Address`, `Contacts`, `Info`, identifier FROM diploma.tbl_personel left join diploma.tbl_station as s on `Station` = s.id "
								+ "WHERE `User` = "
								+ String.valueOf(users.getId()))
				.setResultTransformer(Transformers.aliasToBean(Stations.class))
				.list();
		closeSession();
		return list;
	}

}
